import { post } from '../../utils/fetch'
import * as commonService from '../common'
import { profitMainMsg,profitDetailMsg,profitCompare, profitMainChangeSearch  } from '../../utils/api'
import * as Libs from '../../utils/Libs'
import * as _ from 'lodash'

export const getMainMsg = async (params) => {
  return post(profitMainMsg(params), {})
}

export const getProfitDetailMsg = async (params) => {
  return post(profitDetailMsg(params),{})
}

export const getProfitMainChangeSearch = async (params) => {
  return post(profitMainChangeSearch(params), {})
}

export const getProfitModelCompare = async (params) => {
  return post(profitCompare(params), {})
}

export const buildYySr = (tit, total, xq, yycbTotal, yycbXq) => {
  const linked = commonService.getLinked(total.code) || {}
  let params = Object.assign({}, total, linked)
  // const _linked = v.isDetails === 1 ? params : undefined
  const _linked = params
  const _title = { //营业利润title
    name: tit,
    amount: total.sumMoney || 0,
    linked: _linked,
    yoy: (total.onRise || 0) + '%',
    qoq: (total.linkRise || 0) + '%'
  }
  let _content = []
  _.map(xq, (v) => {
    _content.push(geneContent(v))
  })
  // _content.push(geneCB(yycbTotal, yycbXq))
  return {
    title: _title,
    content: _content
  }
}

export const buildD = (tit, total, xq) => {
  const linked = commonService.getLinked(total.code) || {}
  let params = Object.assign({}, total, linked)
  const _title = { //营业利润title
    name: tit,
    amount: total.sumMoney || 0,
    linked: params,
    yoy: (total.onRise || 0) + '%',
    qoq: (total.linkRise || 0) + '%'
  }
  let _content = []
  _.map(xq, (v) => {
    _content.push(geneContent(v))
  })
  return {
    title: _title,
    content: _content
  }
}

const geneContent = (v) => {
  const linked = commonService.getLinked(v.code) || {}
  let params = Object.assign({}, v, linked)
  // const _linked = v.isDetails === 1 ? params : undefined
  const _linked = params
  return {
    name: v.codeName,
    amount: Libs.numberUpperFormat(v.sumMoney || 0),
    linked: _linked,
    yoy: (v.onRise || 0)+ "%",
    qoq: (v.linkRise || 0) + "%",
  }
}

const geneCB = (total, xq = []) => {
  let _children = []
  _.map(xq, (v) => {
    _children.push(geneContent(v))
  })
  // const _linked = v.isDetails === 1 ? params : undefined
  return {
    name: "营业成本",
    amount: Libs.numberUpperFormat(total.sumMoney || 0),
    linked: undefined,
    style:{fontSize: 14, color: 'red'},
    yoy: (total.onRise || 0)+ "%",
    qoq: (total.linkRise || 0) + "%",
    children: _children
  }
}
